package igor.mass;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;

import com.google.gson.Gson;

import igor.mass.comm.CommunicationClient;
import igor.mass.model.AndroidCommand;
import igor.mass.model.AndroidDevice;
import igor.mass.model.AndroidParam;
import igor.mass.model.AndroidRoom;
import igor.mass.render.renderables.BooleanRenderable;
import igor.mass.render.renderables.IntegerRenderable;
import igor.mass.render.renderables.SliderRenderable;
import igor.mass.render.renderables.StringRenderable;
import igor.mass.render.renderers.AndroidRenderer;
import igor.mass.render.renderers.RendererFactory;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;
import igor.mass.R;

public class CommandActivity extends Activity implements OnClickListener{
	AndroidRoom room;
	AndroidDevice device;
	AndroidCommand command;
	LinearLayout layout;
	ArrayList<AndroidRenderer> renderers;
	HashMap<Integer, String> initialData;
	Button okButton;
	ScrollView scrollView;
	RelativeLayout outerLayout;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    renderers = new ArrayList<AndroidRenderer>();
	    
	    Intent intent = getIntent();
	    room = (AndroidRoom)intent.getSerializableExtra("room");
	    device = (AndroidDevice)intent.getSerializableExtra("device");
	    command = (AndroidCommand)intent.getSerializableExtra("command");
	    initialData = getInitialDataForCommand(device.id,command.id);
	    
	    outerLayout = new RelativeLayout(this);
	    scrollView = new ScrollView(this);
	    layout = new LinearLayout(this);
	    //layout.setBackgroundColor(0xFF252525);
	    layout.setOrientation(LinearLayout.VERTICAL);
	    layout.setBackgroundResource(R.color.background);
	    LayoutParams matchmatch = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
	    setContentView(outerLayout, matchmatch);
	    outerLayout.addView(scrollView, matchmatch);
	    scrollView.addView(layout, matchmatch);
	    scrollView.setBackgroundResource(R.color.background);
	    setTitle(command.name+" for "+device.name+" in "+room.name);
	    
	    int[] parentColors = {0xFF323232, 0xFFFF4848, 0xFF323232}; 
	    
	    LinearLayout.LayoutParams divp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
	    divp.height=1;
	   
	    AndroidRenderer r;
	    ImageView separator;
	    
	    for(AndroidParam param:command.params){
		    r=RendererFactory.getRendererFor(this, param, initialData.get(param.id));
		    //(param.type.equals("boolean")?true:param.type.equals("int")?55:"testval")   
		    renderers.add(r);
		    layout.addView(r.getRenderable().getView());
		    
		    separator = new ImageView(this);
		    separator.setBackgroundDrawable(new GradientDrawable(Orientation.RIGHT_LEFT, parentColors));
		    layout.addView(separator,divp);
	    }
	    
	    /*LinearLayout.LayoutParams lps = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
	    lps.weight=1f;
	    layout.addView(new View(this),lps);*/
	    
	    RelativeLayout.LayoutParams lpss = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
	    lpss.height=70;
	    lpss.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
	    
	    okButton = new Button(this);
	    okButton.setOnClickListener(this);
	    okButton.setTextSize(20);
	    okButton.setText("OK");
	    okButton.setBackgroundResource(R.drawable.list_background_child);
	    okButton.setTextColor(0xFFFFFFFF);
	    outerLayout.addView(okButton,lpss);
	}
	
	public HashMap<Integer, String> getInitialDataForCommand(int deviceId, int commandId){
		HashMap<Integer, String> data = new HashMap<Integer, String>();
		CommunicationClient cclient = new CommunicationClient();
		String values=cclient.getInitialValues(deviceId, commandId);
		
		Type type = new TypeToken<HashMap<Integer, String>>(){}.getType();
		Gson gson = new Gson();		
		data = gson.fromJson(values, type);
	
		return data;
	}
	
	@Override
	public void onClick(View v) {
		HashMap<Integer, String> commandParamsValues = new HashMap<Integer, String>();
		
		String error="";
		for (AndroidRenderer renderer : renderers) {
			if(!renderer.getValidator().validate()){
				error+=renderer.param.name+":"+renderer.getValidator().getErrorMsg()+"\n";
			} 
			commandParamsValues.put(renderer.param.id, renderer.getRenderable().getValue());
		}
		if (error.length()>0){
			Toast.makeText(this, error, Toast.LENGTH_LONG).show();
			return;
			
		}
		
		CommunicationClient cclient = new CommunicationClient();
		cclient.sendCommand(device.id, command.id, commandParamsValues);
		finish();
	}
	

}
